package com.hdcircles.autoform.presenter;

import android.widget.Button;

import com.google.gson.Gson;
import com.hdcircles.autoform.base.RxPresenter;
import com.hdcircles.autoform.model.bean.FormBean;
import com.hdcircles.autoform.model.bean.MapData;
import com.hdcircles.autoform.model.bean.QuestionBean;
import com.hdcircles.autoform.model.http.HttpResponse;
import com.hdcircles.autoform.model.http.RetrofitHelper;
import com.hdcircles.autoform.presenter.contract.AddSurveyContract;
import com.hdcircles.autoform.util.LogUtil;
import com.hdcircles.autoform.util.RxUtil;
import com.orhanobut.logger.Logger;

import java.util.List;
import java.util.TreeMap;

import javax.inject.Inject;

import rx.Observable;
import rx.Subscription;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * Created by hdcircles on 2016/9/28.
 */

public class AddSurveyPresenter  extends RxPresenter<AddSurveyContract.View> implements AddSurveyContract.Presenter{



    private RetrofitHelper retrofitHelper;

    @Inject
    public AddSurveyPresenter(RetrofitHelper retrofitHelper){

        this.retrofitHelper=retrofitHelper;
    }


    @Override
    public void getQuestion(TreeMap<String,QuestionBean> questionList, List<Button> btNumberList, final android.widget.Button view) {

//        Subscription subscriptionQt= Observable.from(questionList)
//                .compose(RxUtil.<QuestionBean>rxScheduleHelper())
//                .filter(new Func1<QuestionBean, Boolean>() {
//                    @Override
//                    public Boolean call(QuestionBean questionBean) {
//                        return questionBean.getId()==view.getId();
//                    }
//                })
//                .subscribe(new Action1<QuestionBean>() {
//                    @Override
//                    public void call(QuestionBean questionBean) {
//                        mView.setCurrentQuestion(questionBean);
//                    }
//                });
//        addSubscribe(subscriptionQt);
        mView.setCurrentQuestion(questionList.get(view.getText().toString()));

        Subscription subscriptionNum=Observable.from(btNumberList)
                .compose(RxUtil.<Button>rxScheduleHelper())
                .filter(new Func1<Button, Boolean>() {
                    @Override
                    public Boolean call(Button button) {
                        return button!=view;
                    }
                })
                .subscribe(new Action1<Button>() {
                    @Override
                    public void call(Button button) {
                        mView.changeButtonStatus(button);
                    }
                });
        addSubscribe(subscriptionNum);
    }

    @Override
    public void saveFormData(FormBean formBean) {
//        realmHelper.insertForm(formBean);
        Subscription subscription=retrofitHelper.saveForm(formBean)
                .compose(RxUtil.<HttpResponse<String>>rxScheduleHelper())
                .compose(RxUtil.<String>handleResult())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        mView.saveFormSuccess("保存成功");
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.showError(throwable.getMessage());
                    }
                });
        addSubscribe(subscription);
    }

    @Override
    public void createMap(String name) {
        Subscription subscription=retrofitHelper.createMap(name)
                .compose(RxUtil.<MapData>rxScheduleHelper())
                .subscribe(new Action1<MapData>() {
                    @Override
                    public void call(MapData mapData) {
                        Logger.json(new Gson().toJson(mapData));
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtil.e(throwable.getMessage());
                    }
                });
        addSubscribe(subscription);
    }
}
